﻿using Cysharp.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 从绳索跳离（player专用）
/// </summary>
public class FSMStateJumpLeave : FSMStateBase
{
    protected override string getName => "jumpLeave";

    public override bool isLoop { get; } = false;

    public override int priority { get; } = UnitDefine.STATE_PRIORITY_NON_INTERRUPTIBLE;

    private float speed => UnitDefine.BASE_SPEED * fsm.entity.attribute.speed;
    private float riseHeight => UnitDefine.JUMP_LEAVE_RISE_HEIGHT;

    protected override void OnEnter()
    {
        JumpLeave();
    }

    private async void JumpLeave()
    {
        var axis = fsm.parameters.GetParam<float>("horizontalAxis");
        var initialSpeedX = axis * speed;
        var initialSpeedY = fsm.entity.physics.FFMEndSpeed(riseHeight);
        fsm.entity.physics.velocity = new Vector2(initialSpeedX, initialSpeedY);
        LookAt(axis);
        await UniTask.WaitUntil(() => !fsm.parameters.GetParam<bool>("isClimber"));
        isEnd = true;
    }

    private void LookAt(float axis)
    {
        if (axis != 0)
            fsm.entity.animator.direction = new Vector3(axis, 1, 1);
    }
}